package com.haruhinav.controller;

import com.haruhinav.Utils.Constants;
import com.haruhinav.Utils.FileUtil;
import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.Wallpaper;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.service.WallpaperService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.interfaces.RSAKey;
import java.util.*;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private WallpaperService wallpaperService;

//    多文件上传
    @PostMapping("/a/upload/v1")
    public ResultVO upload(@RequestParam("file") List<MultipartFile> files, HttpServletRequest request,
                                       HttpServletResponse response)throws IOException{
        ResultVO result = new ResultVO();

        // path2不可取
    //    String path2 = request.getSession().getServletContext().getRealPath("/");
    //    System.out.println("path2==>"+path2);
        String path = System.getProperty("user.dir")+ Constants.UPLOAD_FILE_RELATIVE_URL;
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
    //    System.out.println("file==>"+file);
        //FileOutputStream output = null;
        ArrayList filenames = new ArrayList();
        try {
            if (files != null && files.size() > 0) {
                for (MultipartFile fileObj : files) {
                    //随机生成一个新名字,转换成string,并将其中的"-"换成""
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    //获取上传文件的后缀
                    String fileName = fileObj.getOriginalFilename();
                    String[] strArray = fileName.split("\\.");
                    int suffixIndex = strArray.length -1;
                    String fileType =strArray[suffixIndex];

                    //String fileType = fileObj.getOriginalFilename().split("\\.")[1]; // 这个方法获取后缀有问题
                    String newFileName = uuid+"."+fileType;

                    try{
                        File dest = new File(file + "\\" + newFileName);

                        fileObj.transferTo(dest);
                        filenames.add(newFileName);
    //                    System.out.println("dest==>"+dest);

                    }catch (Exception e){
                        result.setMsg("上传失败");
                        result.setCode(-1);
                        result.setType("error");
                        e.printStackTrace();
                    }
                }
            }
            result.setMsg("上传成功");
            result.setCode(0);
            result.setType("success");
            result.setData(filenames);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("上传失败");
            result.setCode(-1);
            result.setType("error");
        }
        return result;
    }
}
